import { _decorator, Animation, AudioClip, Collider2D, Component, Contact2DType, EventTouch, Input, input, instantiate, IPhysics2DContact, log, Node, Prefab, Sprite, Vec2, Vec3 } from 'cc';
import { Bullet } from './Bullet';
import { Reward, RewardType } from './Reward';
import { GameManager } from './GameManager';
import { LiefCountUI } from './UI/LiefCountUI';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

enum ShootType {
    None,
    OneShoot,
    TwoShoot,
}

@ccclass('Player')
export class Player extends Component {
    // 子弹发射间隔
    @property
    shootRate: number = 0.5;
    // 

    shootTimer: number = 0;
    @property(Prefab)
    bullet1Prefab: Prefab = null;
    @property(Prefab)
    bullet2Prefab: Prefab = null;
    @property(Node)
    bulletParent: Node = null;
    @property(Node)
    position1: Node = null;
    @property(Node)
    position2: Node = null;
    @property(Node)
    position3: Node = null;
    @property
    shootType: ShootType = ShootType.OneShoot;
    @property(Animation)
    anim: Animation = null;
    @property
    lifeCount: number = 1;
    @property
    animHit: string = "";
    @property
    animDown: string = "";
    @property
    invinceTime: number = 1;// 无敌时间
    invinceTimer: number = 0;//
    isInvince: boolean = false;// 是否无敌
    collider: Collider2D = null;
    @property
    twoShootTime:number = 5; // 两发子弹延续时间
    twoShootTimer:number = 0;// 两发子弹计时器
    @property(LiefCountUI)
    lifeCountUI:LiefCountUI = null;

    private canControl:boolean = true;
    @property(AudioClip)
    bulletAudio:AudioClip = null;
    @property(AudioClip)
    getBomAudio:AudioClip = null;
    @property(AudioClip)
    getDoubleAudio:AudioClip = null;

    protected onLoad(): void {
        // 注册事件 触摸事件
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        // 注册单个碰撞体的回调函数
        this.collider = this.getComponent(Collider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            // collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }

    }
    protected start(): void {
        this.lifeCountUI.onBombChange(this.lifeCount);
    }

    // 注销事件
    onDestroy() {
        // 移除事件
        input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        // 移除事件
        if (this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            // collider.off(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }

    update(deltaTime: number) {
        switch (this.shootType) {
            case ShootType.OneShoot:
                this.oneShoot(deltaTime);
                break;
            case ShootType.TwoShoot:
                this.TwoShoot(deltaTime);
                break;
            default:
                break;
        }
        if (this.isInvince) {
            this.invinceTimer += deltaTime;
            if (this.invinceTimer > this.invinceTime) {
                this.isInvince = false;
            }
        }

    }
    oneShoot(deltaTime: number) {
        if (this.lifeCount <= 0) {
            return;
        }
        AudioMgr.inst.playOneShot(this.bulletAudio,0.1);
        // 发射子弹
        this.shootTimer += deltaTime;
        if (this.shootTimer >= this.shootRate) {
            this.shootTimer = 0;
            const bullet1 = instantiate(this.bullet1Prefab);
            this.bulletParent.addChild(bullet1);

            bullet1.setWorldPosition(this.position1.worldPosition);

        }
    }

    TwoShoot(deltaTime: number) {
        this.twoShootTimer += deltaTime;
        if (this.twoShootTimer > this.twoShootTime) {
            this.transitonToOneShoot();
        }
        AudioMgr.inst.playOneShot(this.bulletAudio,0.1);

        // 发射子弹
        this.shootTimer += deltaTime;
        if (this.shootTimer >= this.shootRate) {
            this.shootTimer = 0;
            const bullet1 = instantiate(this.bullet2Prefab);
            this.bulletParent.addChild(bullet1);
            bullet1.setWorldPosition(this.position2.worldPosition);

            const bullet2 = instantiate(this.bullet2Prefab);
            this.bulletParent.addChild(bullet2);
            bullet2.setWorldPosition(this.position3.worldPosition);

        }
    }
    onTouchMove(event: EventTouch) {
        if (!this.canControl) {
            return;
        }

        const p = this.node.position;

        let targetPosition = new Vec3(p.x + event.getDeltaX(), p.y + event.getDeltaY(), p.z);
        // 边界检测
        if (targetPosition.x < -230) {
            targetPosition.x = -230;
        }
        if (targetPosition.x > 230) {
            targetPosition.x = 230;
        }
        if (targetPosition.y < -380) {
            targetPosition.y = -380;
        }
        if (targetPosition.y > 380) {
            targetPosition.y = 380;
        }
        // 移动
        this.node.setPosition(targetPosition);

    }
    // 碰撞检测
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        const reward = otherCollider.getComponent(Reward);
        if (reward) {
            this.onContactToReward(reward)
        }else{
            this.onCantactToEnemy();
        }

    }
//   碰撞检测敌人
    onCantactToEnemy() {
        // 只在两个碰撞体接触结束时被调用一次
        if (this.isInvince) {
            return;
        }
        this.invinceTimer = 0;
        this.isInvince = true;
        this.changeLifeCount(-1);

        if (this.lifeCount > 0) {
            this.anim.play(this.animHit);
        } else {
            this.anim.play(this.animDown);
        }

        if (this.lifeCount <= 0) {
            this.shootType = ShootType.None;
            if (this.collider) {
                this.collider.enabled = false;
            }
            //    结束游戏
            this.scheduleOnce(() =>{
                GameManager.getInstance().gameOver();
            },0.4);
        }
    }

    changeLifeCount(count:number){
        this.lifeCount += count;
        this.lifeCountUI.onBombChange(this.lifeCount);
    }
    lastReward:Reward = null;
    // 奖励
    onContactToReward(reward:Reward){
        if (this.lastReward == reward) {
            return;
        }
        this.lastReward = reward;
        switch (reward.rewardType) {
            case RewardType.TwoShoot:
                AudioMgr.inst.playOneShot(this.getDoubleAudio,0.1);
                this.transitonToTwoShoot();
                break;
            case RewardType.Bomb:
                AudioMgr.inst.playOneShot(this.getBomAudio,0.1);
                GameManager.getInstance().AddBomb();
                break;
            default:
                console.log("未知的奖励");
                break;
        }
        reward.getComponent(Collider2D).enabled = false;
        reward.getComponent(Sprite).enabled = false;

    }
    // 
    transitonToTwoShoot(){
        this.shootType = ShootType.TwoShoot;
        this.twoShootTimer = 0;

    }
    
    transitonToOneShoot(){
        this.shootType = ShootType.OneShoot;
    }

    disableCanControl(){
        this.canControl = false;
    }
    enableControl(){
        this.canControl = true;
    }
}

